/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.bfh.mobicomp.ui.output;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import ch.bfh.mobicomp.AlarmManager;
import ch.bfh.mobicomp.Mail;
import ch.bfh.mobicomp.ui.in.AlarmActivity;
import ch.bfh.mobicomp.ui.in.SettingsActivity;

/**
 *
 * @author Ich
 */
public class EmailAlarm extends BroadcastReceiver {
private SharedPreferences pref;

    @Override
    public void onReceive(final Context context, Intent intent) {
	Log.d("alarm", "startcommand");
	if (intent.getAction() != null) {
	    Log.d("alarm", "not null");
	    // SERVICE ON
	    if (intent.getAction().equals(AlarmManager.GLOBAL_ALARM_ON)) {
		Log.d("alarm", "on");
		 pref = context.getSharedPreferences(AlarmActivity.SP_NAME,Context.MODE_PRIVATE);
		Mail m = new Mail();
		String[] toArr = {pref.getString("email_address", "")};
		m.setTo(toArr);
		m.setFrom(pref.getString("email_address", ""));
		m.setSubject("THIEF ALARM");
		m.setBody("Email was triggered by THIEF ALARM");

		try {
//        m.addAttachment("/sdcard/filelocation"); 

		    if (m.send()) {
//			Toast.makeText(SettingsActivity.this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
			Log.d("EmailAlarm", "Email sent");
		    } else {
//			Toast.makeText(SettingsActivity.this, "Email was not sent.", Toast.LENGTH_LONG).show();
			Log.d("EmailAlarm", "Could not send email");
		    }
		} catch (Exception e) {
//		    Toast.makeText(SettingsActivity.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
		    Log.d("EmailAlarm", "Could not send email", e);
		}

	    }
	    // SERVICE OFF
	    if (intent.getAction().equals(AlarmManager.GLOBAL_ALARM_OFF)) {
		Log.d("alarm", "OFF");
	    }
	}
    }
}